<?php

class TradeController extends BaseController
{

	/**
	 * Displays a particular model.
	 * @param integer $id the ID of the model to be displayed
	 */
	public function actionView($id)
	{
		$this->render('view',array(
			'model'=>$this->loadModel($id),
		));
	}

	/**
	 * Updates a particular model.
	 * If update is successful, the browser will be redirected to the 'view' page.
	 * @param integer $id the ID of the model to be updated
	 */
	public function actionUpdate($id)
	{
		$model=$this->loadModel($id);
		$model->ztr_exp_time = date("Y-m-d",$model->ztr_exp_time);

		// Uncomment the following line if AJAX validation is needed
		$this->performAjaxValidation($model);

		if(isset($_POST['Trade']))
		{
			$model->attributes=$_POST['Trade'];
			$model->ztr_exp_time = strtotime($model->ztr_exp_time);
			if($_POST['cateid3']>0){
				$model->zca_id = (int)$_POST['cateid3'];
			}elseif($_POST['cateid2']>0){
				$model->zca_id = (int)$_POST['cateid2'];
			}elseif($_POST['cateid1']>0){
				$model->zca_id = (int)$_POST['cateid1'];
			}else{
				//失败跳转
				$this->render('../index/error',array('message'=>'行业分类不能为空，请选择行业分类！'));
				exit;
			}
			
			if($model->save()){
				//成功跳转
				$this->render('../index/success',array(    
		            'message'=>'恭喜，宝贝信息发布成功',    
		            'links'=>array(    
		            		array('求购管理',$this->pre_module_url.'/trade/admin'),
		            		array('继续发布求购',$this->pre_module_url.'/tradeCreate/create'),
		                    array('浏览发布的求购',$this->pre_module_url.'/trade/detal/id/'.$model->ztr_id),        
		                   ),    
					));    
				exit;  
			}		}

		$this->render('update',array(
			'model'=>$model,
		));
	}

	/**
	 * Deletes a particular model.
	 * If deletion is successful, the browser will be redirected to the 'admin' page.
	 * @param integer $id the ID of the model to be deleted
	 */
	public function actionDelete($id)
	{
		$this->loadModel($id)->delete();

		// if AJAX request (triggered by deletion via admin grid view), we should not redirect the browser
		if(!isset($_GET['ajax']))
			$this->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : array('admin'));
	}

	/**
	 * Manages all models.
	 */
	public function actionAdmin()
	{
		$model=new Trade('search');
		$model->unsetAttributes();  // clear any default values
		$model->ztr_type=1;//类型:1求购,2供应,3代理,4加工

		if(isset($_GET['Trade']))
			$model->attributes=$_GET['Trade'];
		//行业分类筛选
		if(isset($_GET['cateid1'])){
			if($_GET['cateid3']>0){
				$model->zca_id = (int)$_GET['cateid3'];
			}elseif($_GET['cateid2']>0){
				$model->zca_id = (int)$_GET['cateid2'];
			}elseif($_GET['cateid1']>0){
				$model->zca_id = (int)$_GET['cateid1'];
			}
		}
		
		$this->render('admin',array(
			'model'=>$model,
		));
	}

	/**
	 * Returns the data model based on the primary key given in the GET variable.
	 * If the data model is not found, an HTTP exception will be raised.
	 * @param integer the ID of the model to be loaded
	 */
	public function loadModel($id)
	{
		$model=Trade::model()->findByPk($id);
		if($model===null)
			throw new CHttpException(404,'The requested page does not exist.');
		return $model;
	}

	/**
	 * Performs the AJAX validation.
	 * @param CModel the model to be validated
	 */
	protected function performAjaxValidation($model)
	{
		if(isset($_POST['ajax']) && $_POST['ajax']==='trade-form')
		{
			echo CActiveForm::validate($model);
			Yii::app()->end();
		}
	}
}
